import java.util.HashMap;
import java.util.Map;

public class SingleNumber {
    //找只出现过一次的数字
    public static void main(String[] args) {
        int[] nums = {4,1,2,1,2};
        System.out.println(singleNumber(nums));
    }
    //用哈希表来做，此处可用hashmap和hashset来做
    public static int singleNumber(int[] nums) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if(!map.containsKey(nums[i])){
                map.put(nums[i],1);
            }else {
                int value = map.get(nums[i]);
                value+=1;
                map.put(nums[i],value);
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if (map.get(nums[i]) == 1){
                return nums[i];
            }
        }
        return -1;
    }
}
